package com.ruoyi.ade.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.ade.domain.Standard;
import com.ruoyi.ade.llm.model.Ade;
import com.ruoyi.ade.tool.AdeJsonFormatUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ade.mapper.EmrMapper;
import com.ruoyi.ade.domain.Emr;
import com.ruoyi.ade.service.IEmrService;

/**
 * ADE DatasetService业务层处理
 *
 * @author wxh
 * @date 2025-06-05
 */
@Service
public class EmrServiceImpl implements IEmrService {
    @Autowired
    private EmrMapper emrMapper;

    private static boolean formating = false;

    /**
     * 对标注语料进行标准化处理
     */
    public boolean fromat() {
        if (formating) return false;

        new Thread(() -> {
            formating = true;

            try {
                List<Emr> emrs = emrMapper.selectEmrList(new Emr());
                for (Emr emr : emrs) {
                    List<Ade> ades = JSONArray.parseArray(format(emr.getAdes()), Ade.class);

                    //验证标注结果是否正确
                    if (!AdeJsonFormatUtils.isvalidate(ades)) {  //如果标注的结构有问题 则标注为non-checked
                        Emr r = new Emr();
                        r.setId(emr.getId());
                        r.setStatus("0");
                        emrMapper.updateEmr(r);
                        continue;
                    }
                    for (Ade ade : ades) {  //挨个处理标注的结果
                        //删除无效的标注结果  ”药物“
                        if (ade.getDrugs().size() == 1 && "药物".equals(ade.getDrugs().get(0))) {
                            ade.setDrugs(new ArrayList<>());
                            Emr r = new Emr();
                            r.setId(emr.getId());
                            r.setAdes(JSONArray.toJSONString(ades));
                            emrMapper.updateEmr(r);
                        }

                        //删除 空字符串药物
                        if (ade.getDrugs().size() == 1 && "".equals(ade.getDrugs().get(0))) {
                            ade.setDrugs(new ArrayList<>());
                            Emr r = new Emr();
                            r.setId(emr.getId());
                            r.setAdes(JSONArray.toJSONString(ades));
                            emrMapper.updateEmr(r);
                        }

                        //如果drugs实体或者不良反应实体中有,号，则需要拆分
                        boolean haveCom = false;
                        for (String drug : ade.getDrugs()) {
                            if (drug.contains(",") || drug.contains("，")) haveCom = true;
                        }
                        for (String reaction : ade.getReactions()) {
                            if (reaction.contains(",") || reaction.contains("，")) haveCom = true;
                        }
                        if (haveCom) {
                            Emr r = new Emr();
                            r.setId(emr.getId());
                            r.setStatus("0");
                            emrMapper.updateEmr(r);
                            break;
                        }

                        //规范化疗药物名称
                        boolean have = false;
                        for (int i = 0; i < ade.getDrugs().size(); i++) {
                            if (ade.getDrugs().get(i).contains("化疗")) {
                                ade.getDrugs().set(i, "化疗药");
                                have = true;
                            }
                        }
                        if (have) {
                            Emr r = new Emr();
                            r.setId(emr.getId());
                            r.setAdes(JSONArray.toJSONString(ades));
                            emrMapper.updateEmr(r);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            formating = false;

        }).start();
        return true;
    }

    private String format(String str) {
        if (StringUtils.isEmpty(str)) return "[]";
        return str;
    }

    /**
     * 查询ADE Dataset
     *
     * @param id ADE Dataset主键
     * @return ADE Dataset
     */
    @Override
    public Emr selectEmrById(Long id) {
        return emrMapper.selectEmrById(id);
    }

    /**
     * 查询ADE Dataset列表
     *
     * @param emr ADE Dataset
     * @return ADE Dataset
     */
    @Override
    public List<Emr> selectEmrList(Emr emr) {
        return emrMapper.selectEmrList(emr);
    }

    /**
     * 随机获取一个EMR
     *
     * @param emr ADE Dataset
     * @return
     */
    public Emr selectOneRandomEmr(Emr emr) {
        return emrMapper.selectOneRandomEmr(emr);
    }

    /**
     * 新增ADE Dataset
     *
     * @param emr ADE Dataset
     * @return 结果
     */
    @Override
    public int insertEmr(Emr emr) {
        return emrMapper.insertEmr(emr);
    }

    /**
     * 修改ADE Dataset
     *
     * @param emr ADE Dataset
     * @return 结果
     */
    @Override
    public int updateEmr(Emr emr) {
        return emrMapper.updateEmr(emr);
    }

    /**
     * 批量删除ADE Dataset
     *
     * @param ids 需要删除的ADE Dataset主键
     * @return 结果
     */
    @Override
    public int deleteEmrByIds(Long[] ids) {
        return emrMapper.deleteEmrByIds(ids);
    }

    /**
     * 删除ADE Dataset信息
     *
     * @param id ADE Dataset主键
     * @return 结果
     */
    @Override
    public int deleteEmrById(Long id) {
        return emrMapper.deleteEmrById(id);
    }
}
